<?php
// 本类由系统自动生成，仅供测试用途
class ZhucaoAction extends Action {

	
	

    //公版
    public function query(){
    	header("Content-Type:text/html; charset=utf-8");
    	$environment=D('Environment');$plan=D('Plan');$ps=D('Ps');$line=D('Line');
      $arr_usross=$environment->setenvironment(MODULE_NAME);$usross=$arr_usross['data'];

      $lineid=$usross['f_usr_lineid'];$this->assign('lineid',$lineid);

      $arr_lineo=$line->getlineobylineid($lineid);$lineo=$arr_lineo['data'];
      
      $arr_planls=$plan->getplanlsbylineid_nocomplete($lineid);$planls=$arr_planls['data'];

      $planlsnw=array();
      foreach($planls as $planv){
        // $arr_data=$ps->getsumvolumebyplanid($planv['planid']);$data=$arr_data['data'];
        // $planv['data']=$data;
        //需求有改变//需要研究哪个部件百分比最低，运输的最慢，因此产生的件-》箱最少
        if($planv['planstat']!=2){array_push($planlsnw,$planv);continue;}
        $arr_psls=$ps->getpslsbyplanid($planv['planid']);$psls=$arr_psls['data'];
        $psrate_ref=0;$pso_ref=array();
        $i=0;
        foreach($psls as $psv){
          $psrate=$psv['psalready']/$psv['psvolume'];
          if($i==0){$psrate_ref=$psrate;$pso_ref=$psv;}
          if($psrate<$psrate_ref){
            $psrate_ref=$psrate;
            $pso_ref=$psv;
          }
          $i++;
        }
        //计算箱数
        $xiang=$pso_ref['psalready']/$pso_ref['partvolumepc'];
        $planv['xiang']=$xiang;
        
        array_push($planlsnw,$planv);
      }
      $this->assign('planls',$planlsnw);

      //目前在运作的那个还有多少时间结束
      $arr_plano_running=$plan->getplano_running($lineid);$plano_running=$arr_plano_running['data'];
      if($plano_running){
        $arr_psls=$ps->getpslsbyplanid($plano_running['planid']);$psls=$arr_psls['data'];
        $psrate_ref=0;$pso_ref=array();
        $i=0;
        foreach($psls as $psv){
          $psrate=$psv['psalready']/$psv['psvolume'];
          if($i==0){$psrate_ref=$psrate;$pso_ref=$psv;}
          if($psrate<$psrate_ref){
            $psrate_ref=$psrate;
            $pso_ref=$psv;
          }
          $i++;
        }
        //计算箱数
        $xiang=$pso_ref['psalready']/$pso_ref['partvolumepc'];
        if($xiang==0){
          $remaintm='刚开始';$light='green';
        }else{
          //计算每秒可以搞多少箱,再用剩余的箱数除以这个速度，就可以算出还剩多少时间
          $planstarttm=strtotime($plano_running['planstarttm']);
          $speed=$xiang/(time()-$planstarttm);
          $remaintm=round(($plano_running['planvolumebox']-$xiang)/$speed);

          //两小时是7200秒 两个半小时是9000秒
          $light='red';
          if($remaintm>9000){
            $light='green';
          }else if($remaintm<=9000&&$remaintm>=7200){

          }else{
            $light='yellow';
            //如果没有确定过下一个就直接按确定了下一个
            $arr_plano_queding=$plan->getquedingplano($line);$plano_queding=$arr_plano_queding['data'];
            if(!$plano_queding){
              $arr_plano_autoqueding=$plan->autoquedingnext($lineid);$plano_autoqueding=$arr_plano_autoqueding['data'];

              $ps->changestatbyqueding($plano_autoqueding['planid']);
            }
            
          }
          $remaintm=changeTimeType($remaintm);
        }
      }else{
        $remaintm='未开始';$light='green';
      }
      $this->assign('remaintm',$remaintm);
      $this->assign('light',$light);

      $tm=date('Y.m.d',time());
      $this->assign('tm',$lineo['linenm'].'-'.$tm);
      $this->assign('banner','主操');
      
      $this->assign('ttl','主操');
      $this->display('query');
  
    }

    public function doqueding(){
      header("Content-Type:text/html; charset=utf-8");
      $plan=D('Plan');$ps=D('Ps');

      $planid=gp()['planid'];
      $lineid=gp()['lineid'];

      
      $arr_str=$plan->changeplanstat($planid,$lineid);$str=$arr_str['data'];

     

      $data['str']=$str;
      $this->ajaxReturn($data,'json');
      
    }
    
    public function dojiapei(){
      header("Content-Type:text/html; charset=utf-8");
      $plan=D('Plan');$ps=D('Ps');

      $planid=gp()['planid'];
      $jiapeishuliang=gp()['jiapeishuliang'];

      //第一步计划表要改
      $plan->jiapei($planid,$jiapeishuliang);
      //第二步ps配送要改
      $ps->jiapei($planid,$jiapeishuliang);

      
      $this->ajaxReturn($data,'json');
      
    }


    public function dowancheng(){
      header("Content-Type:text/html; charset=utf-8");
      $plan=D('Plan');$ps=D('Ps');

      $planid=gp()['planid'];

      $plan->wancheng($planid);

      $ps->wancheng($planid);
      
      $this->ajaxReturn($data,'json');
      
    }
    
    public function showsqlresult(){
      header("Content-Type:text/html; charset=utf-8");

      $sql=D('Sql');

      $environment=D('Environment');
      $arr_usross=$environment->setenvironment(MODULE_NAME);$usross=$arr_usross['data'];

      $sqltxt=gp()['sqltxt'];

      $arr_result=$sql->exesql($sqltxt);$result=$arr_result['data'];
      
      $this->assign('result',$result);
      $this->assign('ttl','结果');
      $this->display('showsqlresult');
  
    }

    public function showsqlresult_table(){
      header("Content-Type:text/html; charset=utf-8");
      $sql=D('Sql');

      $environment=D('Environment');
      $arr_usross=$environment->setenvironment(MODULE_NAME);$usross=$arr_usross['data'];

      $sqltxt=gp()['sqltxt'];

      $arr_result=$sql->exesql($sqltxt);$result=$arr_result['data'];
      
      $this->assign('result',$result);
      $this->assign('ttl','结果_表格');
      $this->display('showsqlresult_table');
  
    }

}